  /**
 * SafeCast logging application
 * GUI part of SafeCastLogging
 
 * by Robin Scheibler, 2011
 * safecast.org
 */

import controlP5.*;         // GUI library to have nice interface

int sizeX = 705;
int sizeY = 370;

ControlP5 controlP5;  // Controller for GUI

// Drop down menus
DropdownList gpsMenu, geigerMenu; // DropDown lists for GPS, Geiger
DropdownList gpsDevMenu;  // DropDown to choose GPS device name
DropdownList measureMenu; // DropDown to choose measurement type (air, ...)

// continuous log file name
Textfield logfileTextField; // logfile name
Textarea statusDisplay; // display status messages
Textarea nicePanel; // display status messages

// point log file name
Textfield comment; // display status messages

void init_GUI()
{
  // GUI Controller
  controlP5 = new ControlP5(this);

  // Create Textfield for log file name
  logfileTextField = controlP5.addTextfield("logfilenamett",10,130,400,20);
  logfileTextField.setFocus(true);
  logfileTextField.setLabel("Log file name");
  logfileTextField.setText(logfilename);
  logfileTextField.setAutoClear(false);

  // Start Button!
  controlP5.addButton("start", 0, 580, 130, 55, 20);
  controlP5.addButton("stop", 0, 640, 130, 55, 20);
  controlP5.addButton("connect", 0, 580, 10, 55, 20);
  controlP5.addButton("disconnect", 0, 640, 10, 55, 20);
  controlP5.addButton("tag", 0, 640, 170, 55, 20);

  // Status Panel
  statusDisplay = controlP5.addTextarea("statusDisplay",
    "Welcome to SafeCastLogging!\n",
    10, 210, 550, 150);
  statusDisplay.setColorBackground(color(60));

  // Info display Panel
  nicePanel = controlP5.addTextarea("nicePanel",
    "GEIGER+GPS DATA\n",
    10, 40, 280, 80);
  nicePanel.setColorBackground(color(11));

  // tag comment interface
  comment = controlP5.addTextfield("comment", 10, 170, 550, 20);
  comment.setColorBackground(color(60));
  comment.setLabel("Comment");
  comment.setAutoClear(false);

  // Create DD menu for GPS Device Type
  gpsDevMenu = controlP5.addDropdownList("gpsDevMenu", 10,30,100,120);
  createDropDown(gpsDevMenu, "GPS Device", gpsDeviceTypes);

  // Create DD menu for GPS Serial Device
  gpsMenu = controlP5.addDropdownList("gpsMenu",120,30,160,120);
  createDropDown(gpsMenu, "GPS Serial Device", Serial.list());

  // Create DD menu for Measurement types
  measureMenu = controlP5.addDropdownList("measureMenu",290,30,100,120);
  createDropDown(measureMenu, "Measurement Type", measureTypes);

  // Create DD menu for Geiger Serial Device
  geigerMenu = controlP5.addDropdownList("geigerMenu",400,30,160,120);
  createDropDown(geigerMenu, "Geiger Serial Device", Serial.list());
}

void createDropDown(DropdownList ddl, String name, String[] items) {
  ddl.setBackgroundColor(color(190));
  ddl.setItemHeight(20);
  ddl.setBarHeight(15);
  ddl.captionLabel().set(name);
  ddl.captionLabel().style().marginTop = 3;
  ddl.captionLabel().style().marginLeft = 3;
  ddl.valueLabel().style().marginTop = 3;
  for(int i=0 ; i < items.length ; i++) {
    ddl.addItem(items[i], i);
  }
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255,128));
}

void controlEvent(ControlEvent theEvent) {
  // PulldownMenu is if type ControlGroup.
  // A controlEvent will be triggered from within the ControlGroup.
  // therefore you need to check the originator of the Event with
  // if (theEvent.isGroup())
  // to avoid an error message from controlP5.

  if (theEvent.isGroup()) {
    // check if the Event was triggered from a ControlGroup
    //println("Hi from Group.");
    //println(theEvent.group().value()+" from "+theEvent.group());
    String EName = theEvent.name();
    if (EName.equals("gpsDevMenu"))
    {
      gpsDevId = int(theEvent.group().value());
    }
    else if (EName.equals("gpsMenu"))
    {
      GPSSerialId = int(theEvent.group().value());
    }
    else if (EName.equals("measureMenu"))
    {
      measureId = int(theEvent.group().value());
    }
    else if (EName.equals("geigerMenu"))
    {
      GeigerSerialId = int(theEvent.group().value());
    }
    else if (EName.equals("logfilenamett"))
    {
      logfilename = logfileTextField.getText();
      //println(logfilename);
      logfileTextField.setText(logfilename);
    }
    else if (EName.equals("statusDisplay"))
    { }
  } else if(theEvent.isController()) {
    //println("Hi from Controller.");
    //println(theEvent.controller().value()+" from "+theEvent.controller());
    String EName = theEvent.controller().name();
    if (EName.equals("logfilenamett"))
    {
      logfilename = theEvent.controller().stringValue();
      logfileTextField.setText(logfilename);
      postStatusMessageln("Filename set to " + logfilename);
    } 
    else if (EName.equals("start"))
    { 
      if (connected == true && logging == false)
        startLogging();
      else if (connected == false)
        postStatusMessageln("*** bGeigie need to be connected.");
    }
    else if (EName.equals("stop"))
    { 
      if (logging == true)
        stopLogging();
    }
    else if (EName.equals("connect"))
    { 
      // first check parameters
      if (checkParam() == 0)
      {
        postStatusMessageln("*** Couldn't connect bGeigie...");
      } else {
        // start the serial connection
        initSerial();
        postStatusMessageln("bGeigie connected.");
        connected = true;
      }
    }
    else if (EName.equals("disconnect"))
    { 
      // stop logging if necessary
      if (logging == true)
        stopLogging();
      // mark disconnected
      connected = false;
      // try to stop serial
      stopSerial();
    }
    else if (EName.equals("tag") || EName.equals("comment"))
    { 
      // if connected just record
      if (connected)
      {
        String tag = comment.getText();
        tagLogFile.println(formatLogEntry() + "," + tag.replace(',',' '));
        tagLogFile.flush();
        comment.setText("");
        postStatusMessageln("Tagged current location.");

      }
    }
  }
}

